let app = getApp()
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressShow: false,
        ageShow: false,
        topNavigationHeight: 0,
        selectIndex: 0,
        selectResult: [],
        isAgress: false,
        formData: {
            name: '',
            sex: '',
            birthday: "",
            // phone:"",
            // email:""
        },

        sexs: ['男', '女'],

        currentDate: new Date(),
        minDate: new Date(1950, 1, 1),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if (type == 'day') {
                return `${value}日`;
            }
            return value;
        }
    },

    //更新用户信息
    submit() {
        // if (!this.data.isAgress) {
        //     wx.showToast({
        //         title: '请先勾选同意协议',
        //         icon: 'none'
        //     })
        //     return
        // }
        let formData = this.data.formData
        formData.sex = formData.sex == '' ? '' : formData.sex == '男' ? 1 : 2
        Controller.updateInfo(formData).then(res => {
            wx.showToast({
                title: "更新成功"
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                })
            }, 1200)
        })
    },

    //获取用户信息
    info() {
        Controller.info().then(res => {
            if (res.data.baseData) {
                let { name, sex, birthday } = res.data.baseData
                sex = sex == '' ? '' : sex == 1 ? '男' : '女'
                let formData = { name, sex, birthday }
                this.setData({
                    formData
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.info()
    },

    selectSendBtn(e) {
        let index = e.target.dataset.index

        if (!index) {
            return
        }

        this.setData({
            selectIndex: index
        })
    },
    // 选择是否同意条约
    handleAgree(e) {
        this.setData({
            isAgress: !this.data.isAgress
        })
        console.log(this.data.isAgress)
    },

    // 输入触发
    change(e) {
        let type = e.target.dataset.type
        let value = e.detail.value
        let that = this

        if (!type) {
            return
        }


        switch (type) {
            case 'name':
                that.setData({
                    'formData.name': value
                })
                break;

            case 'phone':
                that.setData({
                    'formData.phone': value
                })
                break;
            case 'email':
                that.setData({
                    'formData.email': value
                })
                break;
            default:


        }



    },

    selectSex(e) {
        let value = e.detail.value
        this.setData({
            'formData.sex': value
        }, () => {
            this.closePopup()
        })
    },
    // 打开弹窗
    openPopup(e) {
        console.log(e)
        let type = e.target.dataset.type
        if (!type) {
            return
        }
        this.setData({
            show: true,
            type
        })
    },
    // 关闭弹窗
    closePopup(e) {
        this.setData({
            show: false
        })
    },
    // 确定选择地址
    confirmDate(e) {
        let time = e.detail
        let date = util.formatTime_2(Number(time), '-')
        this.setData({
            'formData.birthday': date
        })

        this.closePopup()

    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.setData({
            topNavigationHeight: app.globalData.topNavigationHeight + "rpx"
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})